<?php
/**
 * 权限组控制器
 * User: xuyiping（xuyiping@liangzihuzhu.com.cn）
 * Date: 2017/11/30
 * Time: 10:27
 */
namespace App\Http\Controllers\Admin\System;

use App\Http\Controllers\Controller;
use App\Model\User\PermissionGroupModel;
use App\Model\User\PermissionModel;
use Illuminate\Http\Request;

class PermissionGroupController extends Controller
{
    public function index(){
        return view('Admin/system/permission/group');
    }

    /*
     * 列表查询
     */
    public function search(Request $request){

        $where = [];

        //查询条件
        if ($request->input('group_name')) {
            $where['group_name'] = $request->input('group_name');
        }

        $groups = PermissionGroupModel::where($where)
            ->orderBy($request->input('sort'), $request->input('order'))
            ->paginate($request->input('limit'))
            ->toArray();

        //返回数组
        $res_data = array(
            'total' => $groups['total'],
            'rows' => array()
        );

        foreach ( $groups['data'] as &$row ) {

            $operation = '<a href="javascript: void(0);" onclick="PermissionGroup.edit(' . $row['group_id'] . ');">修改</a>&nbsp&nbsp';

            $res_data['rows'][] = array(
                'operation'             =>   $operation,
                'group_name'            =>   $row['group_name'],
                'short_name'            =>   $row['short_name'],
                'sort'            =>   $row['sort'],
                'min_permission_id'    =>   $row['min_permission_id'],
                'max_permission_id'    =>   $row['max_permission_id']
            );
        }

        return response()->json($res_data);
    }

    /**
     * 查询权限组数据
     * @param int $id 权限组id
     * @return \Illuminate\Http\JsonResponse
     */
    public function get($id)
    {

        $group = PermissionGroupModel::find($id);

        if ($group) {
            return response()->json([
                'code' => 200,
                'message' => 'ok',
                'data' => $group
            ]);
        } else {
            return response()->json([
                'code' => 404,
                'message' => '权限组信息没有找到'
            ]);
        }

    }

    /**
     * 新增或修改权限组信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {

        $group_id = $request->input('group_id');
        $group_name = $request->input('group_name');
        $short_name = $request->input('short_name');
        //$icon = $request->input('icon');
        //$active_icon = $request->input('active_icon');
        $sort = $request->input('sort');
        $min_permission_id = $request->input('min_permission_id');
        $max_permission_id = $request->input('max_permission_id');

        //验证单子
        if (empty($group_name)) {
            return response()->json(['code' => 100001, 'message' => '权限组名称没有填写']);
        }

        if (empty($short_name)) {
            return response()->json(['code' => 100002, 'message' => '权限组简称没有填写']);
        }

        if (empty($sort)) {
            return response()->json(['code' => 100005, 'message' => '权限组序号没有填写']);
        }

        if (empty($min_permission_id) || !is_numeric($min_permission_id)) {
            return response()->json(['code' => 100006, 'message' => '最小权限ID必须是数字']);
        }

        if (empty($max_permission_id) || !is_numeric($max_permission_id)) {
            return response()->json(['code' => 100007, 'message' => '最大权限ID必须是数字']);
        }

        if ($min_permission_id > $max_permission_id) {
            return response()->json(['code' => 100008, 'message' => '最大权限ID不能小于最小权限ID']);
        }

        //新增或修改角权限组信息
        if (!empty($group_id)) {

            $groups = PermissionGroupModel::find($group_id);
            //权限组名字发生变化时
            if ($groups->group_name != $group_name) {
                //改变权限表里相应的权限组名
                PermissionModel::where('permissionGroupID', $group_id)->update(['permissionGroupName' => $group_name]);
            }
        } else {   //新增

            //排除重复性
            if(PermissionGroupModel::where('group_name',$group_name)->first()){
                return response()->json([
                    'code' => 100009,
                    'message' => '权限组名称重复'
                ]);
            }

            //简称做展示，不可重复
            if(PermissionGroupModel::where('short_name',$short_name)->first()){
                return response()->json([
                    'code' => 100010,
                    'message' => '权限组简称已存在'
                ]);
            }
            $groups = new PermissionGroupModel();
            $groups->uuid = makeUuid();
            $groups->timeStamp = date("Y-m-d H:i:s");
            $groups->createTime = date("Y-m-d H:i:s");
            $groups->creator = session('ADMIN_USER_ID');
            $groups->permission_num = 0;
        }

        $groups->group_name = $group_name;
        $groups->short_name = $short_name;
        //$groups->icon = $icon;
        //$groups->active_icon = $active_icon;
        $groups->sort = $sort;
        $groups->min_permission_id = $min_permission_id;
        $groups->max_permission_id = $max_permission_id;
        $groups->save();
        return response()->json([
            'code' => 200,
            'message' => 'ok'
        ]);

    }
}